<html>
<head><meta charset="utf-8"><title>How to implement this diagnostic?  (--check-cfg) · t-compiler/wg-diagnostics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/index.html">t-compiler/wg-diagnostics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html">How to implement this diagnostic?  (--check-cfg)</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="216635753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216635753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216635753">(Nov 13 2020 at 16:29)</a>:</h4>
<p>Hello, all. I'm working on implementing this draft RFC: <a href="https://github.com/sivadeilra/rfcs/blob/user/ardavis/checked_cfg/text/0000-conditional-compilation-checking.md">https://github.com/sivadeilra/rfcs/blob/user/ardavis/checked_cfg/text/0000-conditional-compilation-checking.md</a></p>
<p>I have the functionality working, but what's the best way to report diagnostics for this? This RFC specs checking <code>#[cfg(...)]</code> conditions against a list of valid options. So when we're evaluating <code>#[cfg(...)]</code> attributes, the code correctly detects invalid names, like misspelling a feature name. </p>
<p>Should this report a warning, like Ennnn?  Should it report a lint?</p>
<p>If it should be a warning, that's easy for me to implement (I've already implemented that). But can warnings be controlled through <code>#![allow(...)]</code>, etc.? If warnings cannot be managed, then this might not be the best way to report diagnostics for this feature. Then again, since this feature is already an opt-in feature (you enable it by passing flags on the command line, which Cargo will do automatically), then maybe that's perfectly fine.</p>
<p>If the best thing is a lint, like <code>invalid_cfg_name</code> or some such, then how do I do that? I see the mechanisms for reporting an "early" lint, but these appear to attach a delayed error to a particular AST node. The trouble is, if <code>#[cfg(...)]</code> evaluates to "don't include this", then that AST node gets dropped, and there's no good place to hang the diagnostic.</p>



<a name="216636156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636156">(Nov 13 2020 at 16:32)</a>:</h4>
<p>One thing that could work is to attach it to the parent node</p>



<a name="216636303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636303">(Nov 13 2020 at 16:33)</a>:</h4>
<p>Would that be the span that the error report points to? Or would it still have the correct span of the <code>#[cfg]</code> attribute?</p>



<a name="216636321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636321">(Nov 13 2020 at 16:33)</a>:</h4>
<p>I think it is reasonable to expect <code>#[allow(invalid_cfg_name)]#[cfg(...)]some_item</code> not to work, but require you to use <code>#![allow(invalid_cfg_name)]</code> (note the bang) or attach it to an outer node</p>



<a name="216636354"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636354" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636354">(Nov 13 2020 at 16:33)</a>:</h4>
<p>the span would stay correct, that is independent</p>



<a name="216636390"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636390" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636390">(Nov 13 2020 at 16:33)</a>:</h4>
<p>we need the node to figure out <code>#[allow]</code> attributes and such</p>



<a name="216636445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636445">(Nov 13 2020 at 16:33)</a>:</h4>
<p>I guess the only problem could be nested cfgs</p>



<a name="216636565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216636565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216636565">(Nov 13 2020 at 16:34)</a>:</h4>
<p>gotta try this out... writing some code that emits an early lint within something that gets cfg-ed out</p>



<a name="216637541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216637541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Arlie Davis <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216637541">(Nov 13 2020 at 16:40)</a>:</h4>
<p>Right, agreed. Would you have a second to look at my current branch?  <a href="https://github.com/sivadeilra/rust/commits/user/ardavis/check_cfg">https://github.com/sivadeilra/rust/commits/user/ardavis/check_cfg</a></p>
<p>There are 2 commits. The most recent (1976a0a) converts the diagnostics to use lints, but I haven't yet figured out how to do this "correctly", so that the lints are controllable using <code>#![allow(...)]</code>.</p>



<a name="216723232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/147480-t-compiler/wg-diagnostics/topic/How%20to%20implement%20this%20diagnostic%3F%20%20%28--check-cfg%29/near/216723232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/147480-t-compiler/wg-diagnostics/topic/How.20to.20implement.20this.20diagnostic.3F.20.20(--check-cfg).html#216723232">(Nov 14 2020 at 11:23)</a>:</h4>
<p>I think you'll want to add a new variant to <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.BuiltinLintDiagnostics.html">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_lint_defs/enum.BuiltinLintDiagnostics.html</a> and use <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/struct.ParseSess.html#method.buffer_lint_with_diagnostic">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/struct.ParseSess.html#method.buffer_lint_with_diagnostic</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>